﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class QuestionPosting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDown();

        }
    }
     private void BindDropDown() 
    {
        ddlCatagory.DataSource = OnlineexamHelper.context.sp_tblcategoryNewSelectCommand();
        ddlCatagory.DataTextField = "Category";
        ddlCatagory.DataValueField = "CategoryId";
        ddlCatagory.DataBind();
        ddlCatagory.Items.Add("Other");
        ddlCatagory.Items.Insert(0, new ListItem("-Select-", "0"));
        ddlPractice.DataSource = OnlineexamHelper.context.sp_tblPracticeNewSelectCommand();
        ddlPractice.DataTextField = "PracticeTest";
        ddlPractice.DataValueField = "PracticeTestId";
        ddlPractice.DataBind();
        ddlPractice.Items.Add("Other");
        ddlPractice.Items.Insert(0, new ListItem("-Select-", "0"));
        ddlContent.DataSource = OnlineexamHelper.context.sp_tblContentNewSelectCommand();
        ddlContent.DataTextField = "ContentName";
        ddlContent.DataValueField = "ContentId";
        ddlContent.DataBind();
        ddlContent.Items.Add("Other");
        ddlContent.Items.Insert(0, new ListItem("-Select-", "0"));
        ddlLevel.DataSource = OnlineexamHelper.context.sp_tblDlevelNewSelectCommand();
        ddlLevel.DataTextField = "DiffLevel";
        ddlLevel.DataValueField = "LevelId";
        ddlLevel.DataBind();
        ddlLevel.Items.Add("Other");
        ddlLevel.Items.Insert(0, new ListItem("-Select-", "0"));
        ddlSkills.DataSource = OnlineexamHelper.context.sp_tblSkillsetNewSelectCommand();
        ddlSkills.DataTextField = "SkillSet";
        ddlSkills.DataValueField = "SkillId";
        ddlSkills.DataBind();
        ddlSkills.Items.Add("Other");
        ddlSkills.Items.Insert(0, new ListItem("-Select-", "0"));
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string path = string.Empty;
        if (fuImage.HasFile)
        {
            path = "~/admin/upload/" + fuImage.FileName;
            fuImage.SaveAs(Server.MapPath(path));
        }

        var obj = OnlineexamHelper.context.sp_tblQuestionNewInsertCommand(txtQuestion.Text, path, Convert.ToInt64(txtAnswer.Text), Convert.ToInt64(ddlCatagory.SelectedValue), Convert.ToInt64(ddlPractice.SelectedValue), Convert.ToInt64(ddlContent.SelectedValue), Convert.ToInt64(ddlSkills.SelectedValue), Convert.ToInt64(ddlLevel.SelectedValue));
        foreach (var item in obj)
        {            
            DataTable dt = (DataTable)ViewState["Answers"];
            int i = 1;
            var ss=OnlineexamHelper.context.sp_tblSolutionNewInsertCommand(txtSolution.Text);
            foreach(var ddd in ss)
            {

                foreach (DataRow gg in dt.Rows)
                {

                    var ans = OnlineexamHelper.context.sp_tblOptionsNewInsertCommand(gg["Answer"].ToString(), i, item.QuestionId, ddd.Solution_Id);
                    int an = Convert.ToInt32(txtAnswer.Text);
                    if (i == an)
                    {
                        using (var context = new OnlineexamDataContext())
                        {
                            foreach (var opt in ans)
                            {
                                var updat = OnlineexamHelper.context.tblQuestions.Single(a => a.QuestionId == item.QuestionId);
                                updat.Fk_Answer = opt.OptionId;
                                context.SubmitChanges();
                            }
                        }
                    }
                }
                i++;
            }
            dt.Clear();
            BindGrid(dt);
        }
        
        lbregister.Text = "Question Posted";
        emptyfield();
    }

    private void emptyfield()
    {
        txtQuestion.Text = string.Empty;
        txtOption.Text = string.Empty;
        txtAnswer.Text = string.Empty;
        txtSolution.Text = string.Empty;
    }
    protected void ddlCatagory_SelectedIndexChanged(object sender, EventArgs e)
    {
        addtopics();
    }
    protected void ddlSkills_SelectedIndexChanged(object sender, EventArgs e)
    {
       addSkill();

    }
    private void addSkill()
    {
        if (ddlSkills.SelectedItem.Text == "Other")
        {
            txtSkills.Visible = true;
            btnSkillAdd.Visible = true;
        }
        else
        {
            txtSkills.Visible = false;
            btnSkillAdd.Visible = false;
        }
    }
    private void addtopics()
    {
        if (ddlCatagory.SelectedItem.Text == "Other")
        {
            txtAdd.Visible = true;
            btnAdd.Visible = true;
        }
        else
        {
            txtAdd.Visible = false;
            btnAdd.Visible = false;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        OnlineexamHelper.context.sp_tblcategoryNewInsertCommand(txtAdd.Text);
        BindDropDown();
        addtopics();
    }

    protected void btnPracticeAdd_Click(object sender, EventArgs e)
    {
        OnlineexamHelper.context.sp_tblPracticeNewInsertCommand(txtPractice.Text);
        BindDropDown();
        addPractice();
    }

    protected void btnAnswerAdd_Click(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)ViewState["Answers"];
        if (dt == null)
        {
            dt = CreateTable();
        }
        DataRow dr = dt.NewRow();
        dr["Answer"] = txtOption.Text;
        dt.Rows.Add(dr);
        BindGrid(dt);
        ViewState["Answers"] = dt;
        txtOption.Text = string.Empty;
    }
    private DataTable CreateTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Answer"));
        return dt;
    }
    private void BindGrid(DataTable dt)
    {
        if (dt != null)
        {
            grdAnswers.DataSource = dt;
            grdAnswers.DataBind();
        }
    }
    protected void grdAnswers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt = (DataTable)ViewState["Answers"];
        dt.Rows.RemoveAt(e.RowIndex);
        BindGrid(dt);
        ViewState["Answers"] = dt;
    }

    protected void ddlPractice_SelectedIndexChanged(object sender, EventArgs e)
    {
        addPractice();
    }

    private void addPractice()
    {
        if (ddlPractice.SelectedValue == "Other")
        {
            txtPractice.Visible = true;
            btnPracticeAdd.Visible = true;
        }
        else
        {
            txtPractice.Visible = false;
            btnPracticeAdd.Visible = false;
        }
    }
    protected void ddlContent_SelectedIndexChanged(object sender, EventArgs e)
    {
        addContent();
    }

    private void addContent()
    {
        if (ddlContent.SelectedItem.Text == "Other")
        {
            txtContent.Visible = true;
            btnConadd.Visible = true;
        }
        else
        {
            txtContent.Visible = false;
            btnConadd.Visible = false;
        }

    }
    protected void ddlLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        addLevel();
    }

    private void addLevel()
    {
        if (ddlLevel.SelectedItem.Text == "Other")
        {
            txtLevel.Visible = true;
            btnLevelAdd.Visible = true;
        }
        else
        {
            txtLevel.Visible = false;
            btnLevelAdd.Visible = false;        
        }
    }


    protected void btnConadd_Click(object sender, EventArgs e)
    {
        OnlineexamHelper.context.sp_tblContentNewInsertCommand(txtContent.Text, Convert.ToInt64(ddlCatagory.SelectedValue));
        BindDropDown();
        addContent();
    }
    protected void btnSkillAdd_Click(object sender, EventArgs e)
    {
        OnlineexamHelper.context.sp_tblSkillsetNewInsertCommand(txtSkills.Text);
        BindDropDown();
        addSkill();
    }
    protected void btnLevelAdd_Click(object sender, EventArgs e)
    {
        OnlineexamHelper.context.sp_tblDlevelNewInsertCommand(txtLevel.Text);
        BindDropDown();
        addLevel();
    }
}
